package com.acoresoft.sm.web.controller;


import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.acoresoft.sm.entity.User;
import com.acoresoft.sm.service.UserService;

@Controller
@RequestMapping("/login")
public class LoginController {
	private Log log=LogFactory.getLog(LoginController.class);
	@Resource
	private UserService userService;
	
	/**
	 * 登录
	 * @return
	 */
	@RequestMapping("toLogin")
	private String toLogin(){
		log.info("加载登录页面");
		return "login";
	}
	

	@RequestMapping("add")
	//@ModelAttribute 可加可不加,url: http://127.0.0.1:8088/ssm/user/add.do
	public String login(User user, HttpServletRequest req, HttpServletResponse response) {
		// 此处调用服务层进行相应的业务操作		
		// 传递对象于下一页面
		req.setAttribute("user", user);
		// 调用服务层进行验证用户,此处只演示功能
		int usize  = userService.checkUserExits(user); 
		System.out.println("用户信息: " + user + ",usize: "+usize);		
		if (usize > 0) {				
			//return "success";
			List<User> useralls = this.userService.getUserList();	
			req.setAttribute("userList", useralls);
			return "userList";
		}
		return "error";
	}

	// @RequestMapping(params= "method=add2",method=RequestMethod.POST)
	// 定义method方法不是必须的
	@RequestMapping(params = "method=add2")
	public ModelAndView login2(@ModelAttribute("user")User user, HttpServletRequest req, HttpServletResponse response) {
		// 使用ModelAndView保存对象于下一页面
		ModelAndView model = new ModelAndView();
		model.addObject("user", user);
		// 调用服务层进行验证用户,此处只演示功能
		int usize  = userService.checkUserExits(user); 
		System.out.println("用户信息2: " + user + ",usize: "+usize);		
		if (usize > 0) {					
			List<User> useralls = userService.getUserList();				
			model.addObject("userList", useralls);
			model.setViewName("userList");			
		} else {
			model.addObject("msg","用户或者密码错误!");  	
			model.setViewName("error");
		}
		return model;
	}	
	
	@RequestMapping(value="add3/{name}/{password}",method=RequestMethod.GET)
	public String login3(@PathVariable("name") String name,@PathVariable("password") String pwd,Model model) {	
		User user = new User();
		user.setName(name);
		user.setPassword(pwd);
		model.addAttribute("user",user);  				
		// 调用服务层进行验证用户,此处只演示功能
		int usize  = userService.checkUserExits(user); 
		System.out.println("用户信息3: "+user + ",size: " + usize);
		if (usize > 0) {	
			return "success";
		}
		//放入model默认把参数存于请求
		model.addAttribute("msg","用户或者密码错误!");  	
		return  "error";
		
		/**
		 * 重定向JSP页面,走出了springmvc配置的view(jsp)
		 * 因为这样说明:model.addAttribute("msg","用户或者密码错误!"); 
		 * 获取不到值了
		 * 不加上.jsp就是这样:http://127.0.0.1:8080/Spring3-Login-Annotaction/index?msg=%E7...		
		 * */		
		//返回到页面是乱码在页面中用${msg}获取不到值,用request.getParams("msg");为乱码
		//return "redirect:index.jsp?msg=用户或者密码错误!";		
	}	


}
